package top.soulblack.purchasesale.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.common.enums.ResultEnum;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.mapper.GoodsMapper;
import top.soulblack.purchasesale.model.Goods;
import top.soulblack.purchasesale.model.VO.GoodsToIdVO;
import top.soulblack.purchasesale.model.VO.GoodsToQueryVO;

import javax.annotation.Resource;
import java.util.List;

/**
 * create by 小川
 */

@Service
@Slf4j
public class GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    public List<Goods> selectAll() {
        return goodsMapper.selectAll();
    }

    public void addGoods(Goods goods) {
        goodsMapper.addGoods(goods);
        return;
    }

    public boolean exist_single(Goods goods) {
        int num  = /*goodsMapper.existGoods(goods.getName())*/ + goodsMapper.exist_only_bar(goods.getOnlyBarCode());
        if (num >= 1) {
            return true;
        }
        return false;
    }

    public boolean exist(Goods goods) {
        int num  = /*goodsMapper.existGoods(goods.getName())*/ + goodsMapper.exist_bar(goods.getBarCode());
        if (num >= 1) {
            return true;
        }
        return false;
    }

    public boolean deleteGoods(String name) {
        int num = goodsMapper.deleteByPrimaryKey(name);
        if (num >= 1) {
            return true;
        }
        return false;
    }

    public List<Goods> selectForName(String goodsName) {
        return goodsMapper.selectForName(goodsName);
    }

    public boolean updateGoods(Goods goods) {
        int num = goodsMapper.updateByPrimaryKey(goods);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public List<GoodsToQueryVO> selectGoodListToQuery(){
        return goodsMapper.selectGoodListToQuery();
    }

    public List<GoodsToQueryVO> selectSKUToQuery(){
        return goodsMapper.selectSKUToQuery();
    }

    public List<GoodsToIdVO> selectToId(){ return goodsMapper.selectToId();}

    public List<GoodsToIdVO> selectFromName(String goodsName){return goodsMapper.selectFromName(goodsName);}

    public List<GoodsToIdVO> selectClear(){return goodsMapper.selectClear();}

    public List<GoodsToIdVO> selectClearFromName(String goodsName){return goodsMapper.selectClearFromName(goodsName);}

    public Goods selectSKUBycolorIdAndSizeIdAndBarCode(String goodsBarCode, Integer sizeId, Integer colorId){
       Goods goods =  goodsMapper.selectSKUBycolorIdAndSizeIdAndBarCode(goodsBarCode,sizeId,colorId);
       if(goods ==null){
           log.error("【查询SKU】没有对应的商品id goodsBarCode{},sizeId{},colorId[}",goodsBarCode,sizeId,colorId);
           throw new PurchaseException(ResultEnum.SKUEMPITY);
       }
       return goods;
    }
}
